#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "aboutdialog.h"
#include <windows.h>
#include <QDebug>



MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    mainWindowInit();

}

void MainWindow::mainWindowInit()
{
    this->setGeometry(200,100,612,520);
    this->setWindowTitle("记事本");
    this->setWindowIcon(QIcon(":/image/111.png"));
    finddg = new  findDialog(this);
    this->saved = true;

    connect(ui->aboutAction, &QAction::triggered, this, &MainWindow::slotShowAboutDlg);
    connect(ui->searchaction, &QAction::triggered, this, &MainWindow::slotsearchDlg);
    connect(ui->lookingaction, &QAction::triggered, this, &MainWindow::slotHelp);
    connect(ui->textEdit,SIGNAL(textChanged()),this,SLOT(onEdited()));
    connect(ui->newaction,SIGNAL(triggered()),this,SLOT(slotNew()));
    connect(ui->openaction,SIGNAL(triggered()),this,SLOT(slotOpen()));
    connect(ui->saveaction,SIGNAL(triggered()),this,SLOT(slotSave()));
    connect(ui->savetoaction,SIGNAL(triggered()),this,SLOT(slotSaveto()));
    connect(ui->exitaction,SIGNAL(triggered()),this,SLOT(slotexit()));
    connect(finddg,SIGNAL(signalfind(QString,bool,bool)),this,SLOT(findAct(QString,bool,bool)));
    connect(ui->datatimeaction,SIGNAL(triggered()),this,SLOT(addtime()));
    connect(ui->fontaction,SIGNAL(triggered()),this,SLOT(slotGetfont()));


    connect(ui->copyaction, &QAction::triggered, ui->textEdit, &QTextEdit::copy);
    connect(ui->cutaction, &QAction::triggered, ui->textEdit, &QTextEdit::cut);
    connect(ui->pasteaction, &QAction::triggered, ui->textEdit, &QTextEdit::paste);
    connect(ui->repealaction, &QAction::triggered, ui->textEdit, &QTextEdit::undo);
    connect(ui->checkallaction, &QAction::triggered, ui->textEdit, &QTextEdit::selectAll);




    Sleep(2000);

}

void MainWindow::slotNew()
{
    if(this->saved){
        ui->textEdit->clear();
        this->saved = true;
    }else{
        int tag = QMessageBox::question(NULL,tr("记事本"),tr("是否保存文件？"),QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        if(tag == QMessageBox::Yes){
                slotSave();
        }
        ui->textEdit->clear();
        this->saved = true;
    }
}
void MainWindow::slotSave()
{
    if(savePath.isEmpty())
    {
        QString sfile_path;
        while(sfile_path.isEmpty())
        {
            QString FileName = this->windowTitle();

            sfile_path = QFileDialog::getSaveFileName(this,tr("另存为"),tr("%1").arg(FileName),tr("(文本文档)*.txt"));
            if(sfile_path.isEmpty())
                return;
        }

        this->savePath = sfile_path;
    }
    QFile txt(this->savePath);
    bool tag = txt.open(QIODevice::WriteOnly);
    if(tag)
    {
        QTextStream out(&txt);
        out.setCodec(QTextCodec::codecForName("unicode"));
        txt.write(ui->textEdit->toPlainText().toStdString().data());
    }else{
        QMessageBox::critical(NULL,tr("错误"),tr("无法打开文件"),QMessageBox::Yes,QMessageBox::Yes);
    }
    this->saved = true;
}

void MainWindow::slotexit()
{
    this->close();
}


void MainWindow::slotSaveto()
{
    this->savePath.clear();

    QString sfile_path;
    QString FileName = this->windowTitle();

    sfile_path = QFileDialog::getSaveFileName(this,tr("另存为"),tr("%1").arg(FileName),tr("(文本文档)*.txt"));
    if(sfile_path.isEmpty())
        return;
    this->savePath = sfile_path;
    QFile txt(this->savePath);
    bool tag = txt.open(QIODevice::WriteOnly);
    if(tag)
    {
        QTextStream out(&txt);
        out.setCodec(QTextCodec::codecForName("unicode"));
        txt.write(ui->textEdit->toPlainText().toStdString().data());
    }else{
        QMessageBox::critical(NULL,tr("错误"),tr("无法打开文件"),QMessageBox::Yes,QMessageBox::Yes);
    }
    this->saved = true;
}

void MainWindow::slotOpen()
{
    if(saved == false)
    {
        int tag = QMessageBox::question(NULL,tr("记事本"),tr("是否保存文件？"),QMessageBox::Yes | QMessageBox::No, QMessageBox::Yes);
        if(tag == QMessageBox::Yes)
        {
            slotSave();
        }
        ui->textEdit->clear();
        this->saved = true;
    }

    QString fileName;
    fileName = QFileDialog::getOpenFileName(this, "选择文件", "F:", "*.txt *.sh *.py *.cpp *.c", 0, QFileDialog::DontUseNativeDialog|QFileDialog::ReadOnly);
       if(!fileName.isEmpty()){
           loadFile(fileName);
       }
}

/* 加载文件的函数 */
void MainWindow::loadFile(QString fileName)
{
    QFile file(fileName);
    if(!file.open(QFile::ReadOnly | QFile::Text)){
        QMessageBox::critical(this,"critical","文件读取失败");
    }else{
        QTextStream in(&file);
        ui->textEdit->setText(in.readAll());
    }
}

void MainWindow::slotShowAboutDlg()
{
    AboutDialog aboutDlg;
    aboutDlg.exec();
}

void MainWindow::slotsearchDlg()
{
    finddg->show();
}

void MainWindow::findAct(QString strfind, bool ignore, bool choose)
{
    QString txt = strfind;
    bool flag;
    if(!txt.isEmpty()){
        qDebug()<<txt;
        if(choose){
            if(ignore){
                flag=ui->textEdit->find(txt,QTextDocument::FindCaseSensitively);
            }else{
                flag=ui->textEdit->find(txt);
            }
         }else{
            if(ignore){
                flag=ui->textEdit->find(txt,QTextDocument::FindBackward|QTextDocument::FindCaseSensitively);
            }else{
                flag=ui->textEdit->find(txt,QTextDocument::FindBackward);
             }
         }
        if(!flag){
             QMessageBox::information(this,tr("结果"),tr("没有匹配"),QMessageBox::Yes);
        }

    }
}

void MainWindow::slotHelp()
{
    const QUrl url("https://support.microsoft.com/zh-cn/windows?ui=zh-CN&rs=zh-CN&ad=CN");
    QDesktopServices::openUrl(url);
}

void MainWindow::slotGetfont()
{
    //QFont getFont(bool *ok, QWidget *parent = nullptr)

    QFont font = QFontDialog::getFont(0, this);
    ui->textEdit->setFont(font);
}

void MainWindow::addtime()
{
    ui->textEdit->append(QDateTime::currentDateTime().toString());
}

//判断是否编辑了文件
void MainWindow::onEdited()
{
    if(ui->textEdit->toPlainText().isEmpty())
        return;
    saved = false;
}



MainWindow::~MainWindow()
{
    delete ui;
}
